package com.lhh.springcommon.config;

import com.lhh.springcommon.config.RedissonConfigurationProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson客户端配置
 * @author 李厚航
 */
@Configuration
@EnableConfigurationProperties({RedissonConfigurationProperties.class})
public class RedissonConfiguration {

    private final RedissonConfigurationProperties redissonConfigurationProperties;

    @Autowired
    public RedissonConfiguration(RedissonConfigurationProperties redissonConfigurationProperties) {
        this.redissonConfigurationProperties = redissonConfigurationProperties;
    }

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setCodec(StringCodec.INSTANCE);
        config.useSingleServer()
                .setAddress(redissonConfigurationProperties.getSingleServerConfig().getAddress())
                .setSubscriptionConnectionMinimumIdleSize(redissonConfigurationProperties.getSingleServerConfig().getSubscriptionConnectionMinimumIdleSize())
                .setSubscriptionConnectionPoolSize(redissonConfigurationProperties.getSingleServerConfig().getSubscriptionConnectionPoolSize())
                .setConnectionMinimumIdleSize(redissonConfigurationProperties.getSingleServerConfig().getConnectionMinimumIdleSize())
                .setConnectionPoolSize(redissonConfigurationProperties.getSingleServerConfig().getConnectionPoolSize())
                .setDnsMonitoringInterval(redissonConfigurationProperties.getSingleServerConfig().getDnsMonitoringInterval())
                .setIdleConnectionTimeout(redissonConfigurationProperties.getSingleServerConfig().getIdleConnectionTimeout())
                .setConnectTimeout(redissonConfigurationProperties.getSingleServerConfig().getConnectTimeout());
        return Redisson.create(config);
    }

}
